import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/order/model/order_size_model.dart';
import 'package:nav_router/nav_router.dart';

import 'package:rflutter_alert/rflutter_alert.dart';

class OrderAddSizeViewModel extends BaseViewModel {
  List<OrderSizeModel> models;
  TextEditingController sizeNameController = TextEditingController();
  OrderAddSizeViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.postList<OrderSizeModel>('home/bale-pub-size-index', null,
        onSuccess: (data) {
      if (data != null) {
        this.models = data;
        this.setState(ViewState.Success);
      } else {
        this.models = List<OrderSizeModel>();
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void selectModel(OrderSizeModel model) {
    model.check = !model.check;
    notifyListeners();
  }

  void showAlert(BuildContext context) {
    Alert(
        context: context,
        title: "新增颜色",
        content: Column(
          children: <Widget>[
            TextField(
              controller: this.sizeNameController,
              style: TextStyle(
                  color: Color(0xff3B4859),
                  fontWeight: FontWeight.w500,
                  decoration: TextDecoration.none,
                  fontSize: AdapterUtils.getSp(14)),
              decoration: InputDecoration(
                  hintText: '请输入您要添加的款式尺码',
                  border: InputBorder.none,
                  hintStyle: TextStyle(
                      color: Color(0xff99A2AD),
                      fontWeight: FontWeight.w500,
                      decoration: TextDecoration.none,
                      fontSize: AdapterUtils.getSp(14))),
            ),
          ],
        ),
        buttons: [
          DialogButton(
            onPressed: () {
              Navigator.pop(context);
              addSizeApi();
            },
            margin: EdgeInsets.all(0),
            child: Text(
              "确定",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          )
        ]).show();
  }

  void addSizeApi() {
    if (this.sizeNameController.text.isEmpty) {
      FLToast.showText(text: '请输入您要添加的款式尺码');
      return;
    }
    var dismiss = FLToast.loading(text: '正在添加...');
    Http.instance.post(
        'home/bale-pub-size-add',
        {
          'name': this.sizeNameController.text,
        },
        onSuccess: (data) {
          FLToast.showSuccess(text: '添加成功');
          this.sizeNameController.text = '';

          requestApi();
        },
        onError: (error) {},
        onFail: (fail) {},
        complete: () {
          dismiss();
        });
  }

  void selectPost(Function callback) {
    List<OrderSizeModel> lists = List<OrderSizeModel>();
    this.models.forEach((element) {
      if (element.check) {
        lists.add(element);
      }
    });
    if (lists.length == 0) {
      FLToast.showText(text: '请选择您要添加的款式尺码');
      return;
    }
    if (callback != null) {
      callback(lists);
      pop();
    }
  }
}
